/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package de.blitzcoder.collide.gui;

import de.blitzcoder.collide.engine.FileOpener;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.Reader;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author blitzcoder
 */
public class FileDropTargetListener implements DropTargetListener {

    public void dragEnter(DropTargetDragEvent dtde) {}
    public void dragOver(DropTargetDragEvent dtde) {}
    public void dropActionChanged(DropTargetDragEvent dtde) {}
    public void dragExit(DropTargetEvent dte) {}

    private void open(File[] files) {
        for(File f:files) {
            FileOpener.open(f);
        }
    }

    public void drop(java.awt.dnd.DropTargetDropEvent evt) {
        try {
            Transferable tr = evt.getTransferable();

            // Prüfen, ob es sich um eine Dateiliste handelt
            if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                evt.acceptDrop(DnDConstants.ACTION_COPY);

                List fileList = (List) tr.getTransferData(DataFlavor.javaFileListFlavor);

                File[] files = new File[fileList.size()];
                fileList.toArray(files);

                open(files);

                evt.getDropTargetContext().dropComplete(true);

            } else {

                // Für KDE und Gnome
                DataFlavor[] flavors = tr.getTransferDataFlavors();
                boolean handled = false;
                for (int i = 0; i < flavors.length; i++) {
                    if (flavors[i].isRepresentationClassReader()) {

                        evt.acceptDrop(DnDConstants.ACTION_COPY);

                        Reader reader = flavors[i].getReaderForText(tr);
                        BufferedReader br = new BufferedReader(reader);

                        List list = new ArrayList();
                        String line = null;
                        while ((line = br.readLine()) != null) {
                            try {
                                // for kde
                                if (new String(new char[] {'\0'}).equals(line))
                                    continue;
                                File file = new File(new URI(line));
                                list.add(file);
                            } catch (Exception ex) {
                            }
                        }
                        File[] files = new File[list.size()];
                        list.toArray(files);
                        open(files);

                        evt.getDropTargetContext().dropComplete(true);
                        handled = true;
                        break;
                    }
                }
                if (!handled) {
                    evt.rejectDrop();
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            evt.rejectDrop();
        }
    }
}